package com.train.servlet;

import com.train.test.HumanStyle;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@WebServlet("/face")
@MultipartConfig
public class FaceServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        try {
            //取图片
            Part filename = request.getPart("img");
            String type = request.getParameter("type");
            InputStream inputStream = filename.getInputStream();
            System.out.println(inputStream);
            String imgUrl = "";
            imgUrl = HumanStyle.human(inputStream, type);
            if(imgUrl!=""){
                System.out.println(imgUrl);
                out.write( "<!DOCTYPE html>\n" +
                        "<html lang=\"en\"><body><img src='"+imgUrl+"'></body>\n" +
                        "</html>");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
